// 1. 引入 Mock 模块
import Mock from 'mockjs'

Mock.setup({ timeout: 1000 }) 

const userArr = Mock.mock({
    'data|30': [
        {
            "id|+1": 0,
            num: "@increment",
            name: "@name",
            gender: "@integer(0, 1)",
            text: "@title(5, 10)",
            date: '@date(yyyy-MM-dd hh:mm:ss)',
            avatar: "@image('300x300', '#508347', #fff, 'Mock.js')",
            address: '@city(true)'
        }
    ]
})


// 1.1 查询列表
Mock.mock('/api/users', 'get', function() {
    return {
        code: 200,
        data: userArr.data,
        msg: 'success, 获取列表成功.'
    }
})

// 2.0 增加用户
Mock.mock('/api/login', 'post', function(data) {

    const { data: user } = JSON.parse(data.body)

    if(user.username !== 'admin') {
        return {
            code: 500,
            msg: '用户名错误.',
            data: null
        }
    } 

    if(user.password !== '123456') {
        return {
            code: 500,
            msg: '密码错误.',
            data: null
        }
    }

    return {
        code: 200,
        msg: '欢迎您，登录成功.',
        data: {
            isLogin: true
        }
    }
})